//
//  UI+Extension.swift
//  Weibo
//
//  Created by apple on 15/12/7.
//  Copyright © 2015年 idcast. All rights reserved.
//

import UIKit

extension UIButton {

    convenience init(imageName: String, backImageName: String? ) {
       self.init()
       
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        if let backImageName = backImageName {
            setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
            setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        }
        
        sizeToFit()
        
    }
    ///遍历构造函数
    convenience init(title: String, backimageName: String, color: UIColor) {
        self.init()
        setTitle(title, forState:.Normal)
        setTitleColor(color, forState: .Normal)
        setBackgroundImage(UIImage(named: backimageName), forState: .Normal)

        sizeToFit()
        
        
    }
    /// 便利构造函数
    ///
    /// - parameter title:     title
    /// - parameter color:     color
    /// - parameter fontSize:  字体大小
    /// - parameter imageName: 图像名称
    ///
    /// - returns: UIButton
    convenience init(title: String, fontSize: CGFloat, color: UIColor, imageName: String?, backColor: UIColor? = nil) {
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        if let imageName = imageName {
        
            setImage(UIImage(named: imageName), forState: .Normal)
        }
        //设置背景颜色
        backgroundColor = backColor
        
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        sizeToFit()
    }
    
}